<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>JSP</title>
	
	<link rel="stylesheet" href="../../css/index2.css" type="text/css"/>
	<script type="text/javascript" src="../../js/jquery-1.8.0.min.js"></script>
	<script src="../../js/time.js" language="javascript"></script>
	<link href="../../css/lanrenzhijia.css" rel="stylesheet" type="text/css" />
	<link rel="stylesheet" href="../../css/info-reg.css" />
	<link rel="stylesheet" href="../../css/WdatePicker.css" />
	</head>
	
	<body >
	   <div class="content">
		   <div class="time"> <span id="h1"></span> <span id="h2"></span><strong>:</strong> <span id="m1"></span> 
		   <span id="m2"></span><strong>:</strong> <span id="s1"></span> <span id="s2"></span> </div>
		   <div class="date" id="currentime"></div>
	   </div>
	   <br>
	   <!-- 显示上班时间 -->
	   <div style="width:100%;">
	   		<table border="0" width="40%" align="center" style="font-size:25px;">
	   			<tr>
	   				<td align="center" colspan="2" style="font-size:30px;">上班时间</td>
	   			</tr>
	   			<tr>
	   				<td align="center">上午:</td>
	   				<td id="tdam" align="center"></td>
	   			</tr>
	   			<tr>
	   				<td align="center">下午:</td>
	   				<td id="tdpm" align="center"></td>
	   			</tr>
	   			<tr>
	   				<td align="center">晚上:</td>
	   				<td id="tdnt" align="center"></td>
	   			</tr>
	   		</table>
	   </div>
	   <br><br><br>
	   <!-- 考勤和补考勤 -->
	   <div class="btn ue-clear" id="noticeTopic3" style="text-align:center;">
		<a style="margin-left:40%;" href="javascript:void(0);" class="confirm" id="check">考勤</a>
	    <a href="javascript:void(0);" class="clear" id="mendCheck">补考勤</a>
		</div>
		<br>
		<!-- 补考勤 -->
		<div class="title" style="display:none;" id="mendCheckTitle"><h2>补考勤</h2></div>
		<div class="main" style="display:none;" id="mendCheckForm">
		    <p class="short-input ue-clear">
		    	<label>申请详情：</label>
		        <textarea id="text" name="content" placeholder="内容长度不能超过50"></textarea>
		    </p>
		</div>
		<div class="btn ue-clear" style="display:none;" id="mendCheckButton">
			<a href="javascript:;" class="confirm" id="confirm">提交</a>
		    <a href="javascript:;" class="clear" id="cancel">取消</a>
		</div>
	</body>
	
	<script type="text/javascript">
		//字符串转为日期类型
		//var date1=new Date(Date.parse("2015/07/21 09:30:00"));
		//var date2=new Date(Date.parse("2015/07/21 09:23:00"))
		//alert((date1-date2)/60000);
		//接受返回对象变量
		var data;
		$(function(){
			//查询上班时间
			$.ajax({
			 type:"POST",
			 url:"startCheckManager.action",
			 data:"method=queryWorkTime",
			 success:function(msg){
			 	$("#tdam").text(msg.ckTime.amTime);
			 	$("#tdpm").text(msg.ckTime.pmTime);
			 	$("#tdnt").text(msg.ckTime.nightTime);
			 	
			 	//传递为全局
			 	data=msg;
			 }
			});
			
			//补考勤按钮
			$("#mendCheck").click(function(){
				$("#mendCheckTitle").show("slow");
				$("#mendCheckForm").show("slow");
				$("#mendCheckButton").show("slow");
			});
			//取消按钮
			$("#cancel").click(function(){
				$("#mendCheckTitle").hide("slow");
				$("#mendCheckForm").hide("slow");
				$("#mendCheckButton").hide("slow");
			});
			//提交按钮
			$("#confirm").click(function(){
				//获取原因
				if($("#text").val()!=null){
					//异步 添加补考勤信息
					$.ajax({
					 type:"POST",
					 url:"startCheckManager.action?method=insertMendCheck",
					 data:"text="+$("#text").val(),
					 success:function(msg){
					 	if(msg==1){
					 		alert("提交成功");
						 	$("#mendCheckTitle").hide("slow");
							$("#mendCheckForm").hide("slow");
							$("#mendCheckButton").hide("slow");
						}
					 }
					});
				}
			});
			
			$("#check").click(function(){
				//判断当天是否为上班日
				var startDate=parseInt(data.pl.planTime.split("-")[0]);
				var endDate=parseInt(data.pl.planTime.split("-")[1]);
				var norDate=$("#currentime").text().substring($("#currentime").text().length-1,$("#currentime").text().length);
				if(norDate=="一"){
					norDate=1;
				}else if(norDate=="二"){
					norDate=2;
				}else if(norDate=="三"){
					norDate=3;
				}else if(norDate=="四"){
					norDate=4;
				}else if(norDate=="五"){
					norDate=5;
				}else if(norDate=="六"){
					norDate=6;
				}else if(norDate=="天"){
					norDate=7;
				}
				if(norDate<=endDate&&norDate>=startDate){
					//获取当前时间
					var nowDate=new Date();
					var year=nowDate.getFullYear();
					var month=nowDate.getMonth()+1;
					var day=nowDate.getDate();
					//获取当前小时和工作小时对比
					//再获取分钟与工作分钟对比 上班前10分钟可以考勤 下班后30分钟可考勤 逾期将不进行处理
					var nowHour=$("#h1").text()+$("#h2").text();
					var nowMin=$("#m1").text()+$("#m2").text();
					//上午 上班和下班时间
					var workAmStartHour="";
					var workAmEndHour="";
					var workAmStartMin="";
					var workAmEndMin="";
					if(data.ckTime.amTime!="无"){
						workAmStartHour=data.ckTime.amTime.substring(0,2);
						workAmEndHour=data.ckTime.amTime.substring(6,8);
						workAmStartMin=data.ckTime.amTime.substring(3,5);
						workAmEndMin=data.ckTime.amTime.substring(9,11);
					}
					var amStartDate=new Date(Date.parse(year+"/"+month+"/"+day+" "+workAmStartHour+":"+workAmStartMin+":"+"00"));
					var amEndDate=new Date(Date.parse(year+"/"+month+"/"+day+" "+workAmEndHour+":"+workAmEndMin+":"+"00"));
					//下午
					var workPmStartHour="";
					var workPmEndHour="";
					var workPmStartMin="";
					var workPmEndMin="";
					if(data.ckTime.pmTime!="无"){
						workPmStartHour=data.ckTime.pmTime.substring(0,2);
						workPmEndHour=data.ckTime.pmTime.substring(6,8);
						workPmStartMin=data.ckTime.pmTime.substring(3,5);
						workPmEndMin=data.ckTime.pmTime.substring(9,11);
					}
					var pmStartDate=new Date(Date.parse(year+"/"+month+"/"+day+" "+workPmStartHour+":"+workPmStartMin+":"+"00"));
					var pmEndDate=new Date(Date.parse(year+"/"+month+"/"+day+" "+workPmEndHour+":"+workPmEndMin+":"+"00"));
					//晚上
					var workNtStartHour="";
					var workNtEndHour="";
					var workNtStartMin="";
					var workNtEndMin="";
					if(data.ckTime.nightTime!="无"){
						workNtStartHour=data.ckTime.nightTime.substring(0,2);
						workNtEndHour=data.ckTime.nightTime.substring(6,8);
						workNtStartMin=data.ckTime.nightTime.substring(3,5);
						workNtEndMin=data.ckTime.nightTime.substring(9,11);
					}
					var ntStartDate=new Date(Date.parse(year+"/"+month+"/"+day+" "+workNtStartHour+":"+workNtStartMin+":"+"00"));
					var ntEndDate=new Date(Date.parse(year+"/"+month+"/"+day+" "+workNtEndHour+":"+workNtEndMin+":"+"00"));
					
					//上午
					if(((amStartDate-nowDate)/60000)<=10&&amStartDate-nowDate>=0){
						//异步 新增一条 员工考勤信息
						$.ajax({
						 type:"POST",
						 url:"startCheckManager.action",
						 data:"method=insert",
						 success:function(amRes){
						 	if(amRes==1){
						 		alert("考勤成功");
						 	}else{
						 		alert("您今天已经考过勤了!");
						 	}
						 }
						});
					}else if(((nowDate-amEndDate)/60000)<=30&&nowDate-amEndDate>=0){
						//异步 修改一条考勤信息
						update("check_amEndTime");
					}
					//下午
					else if(((pmStartDate-nowDate)/60000)<=10&&pmStartDate-nowDate>=0){
						//异步 修改一条考勤信息
						update("check_pmStartTime");
					}else if(((nowDate-pmEndDate)/60000)<=30&&nowDate-pmEndDate>=0){
						//异步 修改一条考勤信息
						if(workNtStartMin==""){
							//晚上不上班,下午即可结束考勤
							update("check_pmEndTime&index=last");
						}else{
							update("check_pmEndTime");
						}
					}
					//晚上
					else if(((ntStartDate-nowDate)/60000)<=10&&ntStartDate-nowDate>=0){
						//异步 修改一条考勤信息
						update("check_ntStartTime");
					}else if(((nowDate-ntEndDate)/60000)<=30&&nowDate-ntEndDate>=0){
						//异步 修改一条考勤信息
						update("check_ntEndTime"+"&index=last");
					}else{
						alert("当前时间不在考勤时间内");
					}
				}else{
					alert("今天不是工作日,不能进行考勤！！");
				}
			});
		});
		function update(type){
			//异步 修改员工考勤信息
			$.ajax({
			 type:"POST",
			 url:"startCheckManager.action?method=update",
			 data:"type="+type,
			 success:function(amRes){
			 	if(amRes==1){
			 		alert("打卡成功");
			 	}else{
			 		alert("您上午未进行考勤,所以今天都不能进行打卡考勤");
			 	}
			 }
			});
		}
	
	
		//更改时间字体颜色
		$("#currentime").css("color","black");
	</script>
</html>
